(function imSdk(window) {
    window.im = {};

    function console(info) {
        return function () {
            window.console.log(info);
        }
    }

    var messageHandler = {};

    im.init = function (token, config) {
        this.ws = new WebSocket(((document.location.protocol == "http:") ? "ws" : "wss") + "://" + window.location.hostname + ":8080?ZMM-SESSION-TOKEN=" + token)
        this.ws.onclose = config.close ? config.close : console("发生on-close事件")
        this.ws.onopen = config.open ? config.open : console("发生on-open事件")
        this.ws.onerror = config.error ? config.error : console("发生on-error事件")
        this.ws.onmessage = function (ev) {
            var msg = JSON.parse(JSON.parse(ev.data).msg);
            if (msg && msg.type) {
                messageHandler[msg.type] ? messageHandler[msg.type](msg) : console("未发现" + ev.data + "的处理器")()
            }
        }
    }

    /**
     * 注册单聊消息接收处理器
     * @param handler
     */
    im.registerSignalHandler = function (handler) {
        messageHandler["signal"] = handler;
    }

    /**
     * 注册好友列表处理器
     * @param handler
     */
    im.registerFriendListHandler = function (handler) {
        messageHandler["friends"] = handler;
    }

    /**
     * 注册自己信息处理器
     * @param handler
     */
    im.registerSelfInfoHandler = function (handler) {
        messageHandler["self"] = handler;
    }

    /**
     * 注册历史消息处理器
     * @param handler
     */
    im.registerHistoryHandler = function (handler) {
        messageHandler["history"] = handler;
    }

    /**
     * 发送单聊消息
     * @param text
     * @param sendId
     * @param receiveId
     * @param extra
     */
    im.sendSignalChat = function (text, sendId, receiveId, extra) {
        var msg = {
            uinId: this.guid(),
            sender: sendId,
            receiver: receiveId,
            msgType: "text",
            content: text,
            type: "singleChat",
            extra: extra ? extra : {}
        };
        var data = {type: "chat", msg: JSON.stringify(msg)};
        this.ws.send(JSON.stringify(data));
        this.records.push(msg)
    }

    /**
     * 发送hello消息
     * @param sendId
     * @param receiveId
     * @param extra
     */
    im.sendHelloChat = function (sendId, receiveId, extra) {
        this.sendSignalChat("[hello]", sendId, receiveId, extra)
    }

    /**
     * 获取自己的信息
     */
    im.selfInfo = function () {
        var data = {type: "self"};
        this.ws.send(JSON.stringify(data));
    }

    /**
     * 获取单聊历史数据
     * @param selfId
     * @param friendId
     * @param lastId
     */
    im.signalChatHistory = function (selfId, friendId, lastId) {
        var msg = {
            self: selfId,
            receiver: friendId,
            type: "singleHistory",
            last: lastId
        };
        var data = {type: "chat", msg: JSON.stringify(msg)};
        this.ws.send(JSON.stringify(data));
    }

    /**
     * 生成随机码
     * @returns {string}
     */
    im.guid = function () {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

})(window)